package com.hyt.it.ogt.ks.controller;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.ks.enums.BizStatusCode;
import com.hyt.it.ogt.ks.enums.LogTypeCode;
import com.hyt.it.ogt.ks.enums.log.CandidateOperatorLogType;
import com.hyt.it.ogt.ks.enums.log.LogEventType;
import com.hyt.it.ogt.ks.model.vo.LogVO;
import com.hyt.it.ogt.ks.model.vo.monitor.response.WxLoginVO;
import com.hyt.it.ogt.ks.service.ILogService;
import com.hyt.it.ogt.ks.util.DateUtil;
import com.hyt.it.ogt.opertaion.track.model.OtherLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "23.日志接口", value = "日志接口")
@ApiSort(23)
@Slf4j
@RestController
@RequestMapping("/ks/log/")
public class LogController {
    @Resource
    private ILogService iLogService;

    @ApiOperation(value = "23.1 记录日志")
    @ApiOperationSort(1)
    @PostMapping("save")
    public ApiResponse save(@RequestBody LogVO logVO) {
        try {
            LogTypeCode type = LogTypeCode.getMsg(logVO.getType());
            OtherLogVO otherLogVO = OtherLogVO.builder().examId(logVO.getExamId()).candidateId(logVO.getCandidateId())
                    .beginTime(DateUtil.getNowStr()).build();
            switch (type) {
                case OPEN_PROMISE:
                    otherLogVO.setEventType(LogEventType.OPERATOR_LOG.getCode());
                    otherLogVO.setOperateType(CandidateOperatorLogType.CANDIDATE_MAKE_BELIEVE.getCode());
                    iLogService.promiseLog(logVO);
                    break;
                case START_EXAM:
                    iLogService.startExamLog(logVO.getExamId(), logVO.getCandidateId());
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("保存日志异常：{}", e);
        }
        return ApiResponse.<WxLoginVO>builder().code(BizStatusCode.KS_SUCCESS_OK.getCode()).build();
    }

}
